home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / cache.js next >
Text File  |  2008-03-13  |  7KB  |  362 lines

  1. /*
  2.     cache.js
  3.  
  4.     Copyright ⌐ 2005, 2006, 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. /* Cache status */
  8. const WOT_QUERY_ERROR = 0;    /* Failed */
  9. const WOT_QUERY_OK    = 1;    /* Successful */
  10. const WOT_QUERY_RETRY = 2;    /* Request or cache timed out, retry */
  11. const WOT_QUERY_LINK  = 3;    /* Incomplete for a query use, retry */
  12.  
  13. const WOT_PREFIX_CACHE = "wot_cache";
  14. const WOT_PREFIX_NONCE = "wot_nonce";
  15. const WOT_CNRE = RegExp(WOT_PREFIX_CACHE + "\:(.+)\:exists");
  16.  
  17. var wot_cache =
  18. {
  19.     init: function()
  20.     {
  21.     },
  22.  
  23.     get_nonce_name: function(nonce)
  24.     {
  25.         if (!nonce) {
  26.             return null;
  27.         }
  28.  
  29.         return WOT_PREFIX_NONCE + ":" + nonce;
  30.     },
  31.  
  32.     add_nonce: function(nonce, name)
  33.     {
  34.         var nn = this.get_nonce_name(nonce);
  35.  
  36.         if (!nn) {
  37.             return;
  38.         }
  39.  
  40.         wot_hashtable.set(nn, name);
  41.     },
  42.  
  43.     resolve_nonce: function(nonce)
  44.     {
  45.         var nn = this.get_nonce_name(nonce);
  46.  
  47.         if (!nn) {
  48.             return null;
  49.         }
  50.  
  51.         return wot_hashtable.get(nn);
  52.     },
  53.  
  54.     remove_nonce: function(nonce)
  55.     {
  56.         var nn = this.get_nonce_name(nonce);
  57.  
  58.         if (!nn) {
  59.             return;
  60.         }
  61.  
  62.         wot_hashtable.remove(nn);
  63.     },
  64.  
  65.     get_property_name: function(name, property)
  66.     {
  67.         if (!name || !property) {
  68.             return null;
  69.         }
  70.  
  71.         var cn = wot_idn.utftoidn(name);
  72.  
  73.         if (!cn) {
  74.             return null;
  75.         }
  76.  
  77.         return WOT_PREFIX_CACHE + ":" + cn + ":" + property;
  78.     },
  79.  
  80.     get: function(name, property)
  81.     {
  82.         if (!wot_prefs.enabled) {
  83.             return null;
  84.         }
  85.  
  86.         var pn = this.get_property_name(name, property);
  87.  
  88.         if (!pn) {
  89.             return null;
  90.         }
  91.  
  92.         return wot_hashtable.get(pn);
  93.     },
  94.  
  95.     set: function(name, property, value)
  96.     {
  97.         var pn = this.get_property_name(name, property);
  98.  
  99.         if (!pn) {
  100.             return;
  101.         }
  102.  
  103.         wot_hashtable.set(pn, value);
  104.     },
  105.  
  106.     remove: function(name, property)
  107.     {
  108.         var pn = this.get_property_name(name, property);
  109.  
  110.         if (!pn) {
  111.             return;
  112.         }
  113.  
  114.         wot_hashtable.remove(pn);
  115.     },
  116.  
  117.     iscached: function(name)
  118.     {
  119.         return !!this.get(name, "exists");
  120.     },
  121.  
  122.     isok: function(name)
  123.     {
  124.         return (this.iscached(name) &&
  125.                 this.get(name, "status") == WOT_QUERY_OK);
  126.     },
  127.  
  128.     get_enumerator: function()
  129.     {
  130.         return wot_hashtable.get_enumerator();
  131.     },
  132.  
  133.     get_name_from_element: function(element)
  134.     {
  135.         try {
  136.             if (!element || !element.QueryInterface) {
  137.                 return null;
  138.             }
  139.  
  140.             var property =
  141.                     element.QueryInterface(Components.interfaces.nsIProperty);
  142.  
  143.             if (!property) {
  144.                 return null;
  145.             }
  146.  
  147.             if (property.name.lastIndexOf(":exists") < 0) {
  148.                 return null;
  149.             }
  150.  
  151.             var match = property.name.match(WOT_CNRE);
  152.  
  153.             if (!match || !match[1]) {
  154.                 return null;
  155.             }
  156.  
  157.             return match[1];
  158.         } catch (e) {
  159.             dump("wot_cache.get_name_from_element: failed with " + e + "\n");
  160.         }
  161.         return null;
  162.     },
  163.  
  164.     create: function(name)
  165.     {
  166.         try {
  167.             if (!name) {
  168.                 return;
  169.             }
  170.  
  171.             var pending = false;
  172.  
  173.             if (this.iscached(name)) {
  174.                 pending = this.get(name, "pending");
  175.             } else {
  176.                 this.set(name, "exists", true);
  177.                 this.set(name, "pending", false);
  178.                 this.set(name, "warned",  0);
  179.             }
  180.  
  181.             this.set(name, "inprogress", false);
  182.             this.set(name, "status", WOT_QUERY_RETRY);
  183.             this.set(name, "time", Date.now());
  184.  
  185.             for (var i = 0; i < WOT_APPLICATIONS; ++i) {
  186.                 this.set(name, "reputation_" + i, -1);
  187.                 this.set(name, "confidence_" + i, -1);
  188.  
  189.                 if (!pending) {
  190.                     this.set(name, "testimony_" + i, -1);
  191.                 }
  192.                 
  193.                 this.set(name, "inherited_" + i, 0);
  194.                 this.set(name, "lowered_" + i, 0);
  195.             }
  196.         } catch (e) {
  197.             dump("wot_cache.create: failed with " + e + "\n");
  198.         }
  199.     },
  200.  
  201.     destroy: function(name)
  202.     {
  203.         try {
  204.             if (!this.iscached(name) || this.get(name, "pending")) {
  205.                 return;
  206.             }
  207.  
  208.             this.remove(name, "exists");
  209.             this.remove(name, "pending");
  210.             this.remove(name, "warned");
  211.             this.remove(name, "inprogress");
  212.             this.remove(name, "status");
  213.             this.remove(name, "time");
  214.  
  215.             for (var i = 0; i < WOT_APPLICATIONS; ++i) {
  216.                 this.remove(name, "reputation_" + i);
  217.                 this.remove(name, "confidence_" + i);
  218.                 this.remove(name, "testimony_" + i);
  219.                 this.remove(name, "inherited_" + i);
  220.                 this.remove(name, "lowered_" + i);
  221.             }
  222.         } catch (e) {
  223.             dump("wot_cache.destroy: failed with " + e + "\n");
  224.         }
  225.     },
  226.  
  227.     add_target: function(nonce, target, islink)
  228.     {
  229.         try {
  230.             var index = target.attributes.getNamedItem(
  231.                             WOT_SERVICE_XML_QUERY_TARGET_INDEX);
  232.  
  233.             if (index && index.nodeValue != null) {
  234.                 nonce += "-" + index.nodeValue;
  235.             }
  236.  
  237.             var name = this.resolve_nonce(nonce);
  238.  
  239.             if (!name) {
  240.                 dump("wot_cache.add_target: unknown nonce: " + nonce + "\n");
  241.                 return;
  242.             }
  243.  
  244.             this.remove_nonce(nonce);
  245.  
  246.             if (!this.iscached(name)) {
  247.                 this.create(name);
  248.             }
  249.  
  250.             var child = target.firstChild;
  251.             var a, r, c, t, i, l;
  252.  
  253.             dump("wot_cache.add_target: caching " + name + "\n");
  254.  
  255.             if (islink) {
  256.                 if (this.get(name, "status") == WOT_QUERY_OK) {
  257.                     dump("wot_cache.add_target: not overwriting on link for " +
  258.                         name + "\n");
  259.                     return;
  260.                 }
  261.                 this.set(name, "status", WOT_QUERY_LINK);
  262.             } else {
  263.                 this.set(name, "status", WOT_QUERY_OK);
  264.             }
  265.  
  266.             while (child) {
  267.                 if (child.localName == WOT_SERVICE_XML_QUERY_APPLICATION) {
  268.                     a = child.attributes.getNamedItem(
  269.                             WOT_SERVICE_XML_QUERY_APPLICATION_NAME);
  270.                     r = child.attributes.getNamedItem(
  271.                             WOT_SERVICE_XML_QUERY_APPLICATION_R);
  272.                     c = child.attributes.getNamedItem(
  273.                             WOT_SERVICE_XML_QUERY_APPLICATION_C);
  274.                     i = child.attributes.getNamedItem(
  275.                             WOT_SERVICE_XML_QUERY_APPLICATION_I);
  276.                     l = child.attributes.getNamedItem(
  277.                             WOT_SERVICE_XML_QUERY_APPLICATION_L);
  278.                     t = child.attributes.getNamedItem(
  279.                             WOT_SERVICE_XML_QUERY_APPLICATION_T);
  280.  
  281.                     if (a && a.nodeValue) {
  282.                         if (r && r.nodeValue && c && c.nodeValue) {
  283.                             dump(name + ": " + a.nodeValue + ": (r, c) = (" +
  284.                                 r.nodeValue + ", " + c.nodeValue + ")\n");
  285.                             this.set(name, "reputation_" + a.nodeValue,
  286.                                 Number(r.nodeValue));
  287.                             this.set(name, "confidence_" + a.nodeValue,
  288.                                 Number(c.nodeValue));
  289.                         }
  290.                         if (i && i.nodeValue) {
  291.                             dump(name + ": " + a.nodeValue + ": (i) = (" +
  292.                                 i.nodeValue + ")\n");
  293.                             this.set(name, "inherited_" + i.nodeValue,
  294.                                 Number(i.nodeValue));
  295.                         }
  296.                         if (l && l.nodeValue) {
  297.                             dump(name + ": " + a.nodeValue + ": (l) = (" +
  298.                                 l.nodeValue + ")\n");
  299.                             this.set(name, "lowered_" + l.nodeValue,
  300.                                 Number(l.nodeValue));
  301.                         }
  302.                         if (t && t.nodeValue) {
  303.                             dump(name + ": " + a.nodeValue + ": (t) = (" +
  304.                                 t.nodeValue + ")\n");
  305.                             this.set(name, "testimony_" + a.nodeValue,
  306.                                 Number(t.nodeValue));
  307.                         }
  308.                     } else {
  309.                         dump("wot_cache.add_target: application name missing\n");
  310.                     }
  311.                 }
  312.  
  313.                 child = child.nextSibling;
  314.             }
  315.         } catch (e) {
  316.             dump("wot_cache.add_target: failed with " + e + "\n");
  317.         }
  318.     },
  319.  
  320.     add_query: function(queries, targets, islink)
  321.     {
  322.         try {
  323.             if (!queries) {
  324.                 dump("wot_cache.add_query: root element missing\n");
  325.                 return;
  326.             }
  327.  
  328.             var q = queries.item(0);
  329.  
  330.             if (!q) {
  331.                 dump("wot_cache.add_query: root element missing\n");
  332.                 return;
  333.             }
  334.  
  335.             var nonce =
  336.                 q.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_NONCE);
  337.  
  338.             if (!nonce || !nonce.nodeValue) {
  339.                 dump("wot_cache.add_query: nonce attribute missing\n");
  340.                 return;
  341.             }
  342.  
  343.             if (!targets) {
  344.                 dump("wot_cache.add_query: target elements missing\n");
  345.                 return;
  346.             }
  347.  
  348.             var i = 0;
  349.             var t = targets.item(0);
  350.  
  351.             while (t) {
  352.                 this.add_target(nonce.nodeValue, t, islink);
  353.                 t = targets.item(++i);
  354.             }
  355.         } catch (e) {
  356.             dump("wot_cache.add_query: failed with " + e + "\n");
  357.         }
  358.     }
  359. };
  360.  
  361. wot_cache.init();
  362.